//
//  LGSportsRecordController.m
//  XieHeHeartDisease
//
//  Created by 1234 on 2024/2/28.
//  运动记录

#import "LGSportsRecordController.h"
//训练记录
#import "LGTrainDetailController.h"
#import "LGSportRecordHeaderView.h"
#import "LGSportRecordChartView.h"
#import "LGSportTrainRecordCell.h"
@interface LGSportsRecordController ()<UITableViewDelegate,UITableViewDataSource,LGSportTrainRecordDelegate,LGSportRecordChartDelegate>
/**   */
@property (nonatomic , strong)LGSportRecordHeaderView *headView;
/**   */
@property (nonatomic , strong)LGSportRecordChartView *chartView;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *monthStr;
/**   */
@property (nonatomic , copy)NSString *isChats;

@end

@implementation LGSportsRecordController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"运动记录";
    self.isChats = @"N";
    self.levelid = (_levelid && _levelid.length>0)?_levelid:@"";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    [RequestUtil withPOST:@"/doctor/Sportsprescription.Prescription/trainRecord" parameters:@{@"patient_id":self.patientid,@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.headView.trainDays = LGNSString(returnData[@"train_days"]);
            self.headView.trainTimes = LGNSString(returnData[@"train_times"]);
            self.chartView.dataDic = returnData[@"column_chats"];
            self.dataArray = returnData[@"traindetail_lists"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}

-(void)selectMonth:(NSString *)month{
    [RequestUtil withPOST:@"/doctor/Sportsprescription.Prescription/trainRecord" parameters:@{@"patient_id":self.patientid,@"date_range":month,@"is_chats":@"Y",@"level_id":self.levelid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView.dataDic = returnData[@"column_chats"];
            self.dataArray = returnData[@"traindetail_lists"];
            [self.tableView reloadData];
        }
    } error:^(NSError *error) {
        
    }];
}


//训练详情
-(void)trainRecord:(NSString *)trainid{
    LGTrainDetailController *controller = [[LGTrainDetailController alloc]init];
    controller.trainid = trainid;
    [self.navigationController pushViewController:controller animated:YES];
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGSportTrainRecordCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.superTableView = self.tableView;
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(44)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGSportTrainRecordCell class] forCellReuseIdentifier:@"cell"];
        _tableView.backgroundColor = [UIColor colorWithString:@"#F3F7F8"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableHeaderView = [self tableHeaderView];
    }
    return _tableView;
}


-(LGSportRecordHeaderView *)headView{
    if(!_headView){
        _headView = [[LGSportRecordHeaderView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(102))];
    }
    return _headView;
}


-(LGSportRecordChartView *)chartView{
    if(!_chartView){
        _chartView = [[LGSportRecordChartView alloc] initWithFrame:CGRectMake(0, kMaxY(self.headView.frame), Screen_W, viewPix(270))];
        _chartView.delegate = self;
    }
    return _chartView;
}

-(UIView *)tableHeaderView{
    UIView *baseView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, viewPix(376))];
    [baseView addSubview:self.headView];
    [baseView addSubview:self.chartView];
    return baseView;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
